<?php

define('FADE_STEPS', 3500);
define('FADE_COUNT', 255);

$socket = fsockopen('udp://192.168.120.30:8888');

$colors = array('r', 'g', 'b', 'w');

for ($loop = 0; $loop < 10; $loop++) {
    foreach ($colors as $key) {
        if ($socket) {
            $value = 'w' . str_pad(0, 3, '0', STR_PAD_LEFT) . "\n";
            echo $value;
            fputs($socket, $value);
            usleep(FADE_STEPS);

            for ($loop = 0; $loop < 50; $loop++) {
                $value = $key . str_pad(0, 3, '0', STR_PAD_LEFT) . "\n";
                echo $value;
                fputs($socket, $value);
                usleep(FADE_STEPS * 25);
                $value = $key . str_pad(255, 3, '0', STR_PAD_LEFT) . "\n";
                echo $value;
                fputs($socket, $value);
                usleep(FADE_STEPS * 25);
            }

            $value = 'w' . str_pad(0, 3, '0', STR_PAD_LEFT) . "\n";
            echo $value;
            fputs($socket, $value);
            usleep(FADE_STEPS);
            for ($loop = 0; $loop < 10; $loop++) {
                for ($i = 1; $i <= FADE_COUNT; $i++) {
                    if ($socket) {
                        $value = $key . str_pad($i, 3, '0', STR_PAD_LEFT) . "\n";
                        echo $value;
                        fputs($socket, $value);
                        usleep(FADE_STEPS);
                    } else {
                        echo "nsck\n";
                    }
                }
                for ($i = FADE_COUNT; $i != 0; $i--) {
                    if ($socket) {
                        $value = $key . str_pad($i, 3, '0', STR_PAD_LEFT) . "\n";
                        echo $value;
                        fputs($socket, $value);
                        usleep(FADE_STEPS);
                    } else {
                        echo "nsck\n";
                    }
                }
            }
        }
    }
}
if ($socket) {
    $value = 'w' . str_pad(0, 3, '0', STR_PAD_LEFT) . "\n";
    echo $value;
    fputs($socket, $value);
    usleep(FADE_STEPS);
}
fclose($socket);
?>
